<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar">
      <van-icon name="cross" slot="left" @click="$router.back()" />
      <span slot="title">登录/注册</span>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form @submit="onSubmit" :show-error="false" ref="form">
      <van-field
        name="mobile"
        v-model="user.mobile"
        placeholder="请输入手机号"
        clearable
        :rules="userFormRules.mobile"
      >
        <i slot="left-icon" class="icon icon-shouji"></i>
      </van-field>

      <van-field
        name="code"
        v-model="user.code"
        placeholder="请输入验证码"
        clearable
        :rules="userFormRules.code"
      >
        <i slot="left-icon" class="icon icon-yanzhengma"></i>
        <template #button>
          <!-- 倒计时组件 -->
          <van-button
            v-if="isShow"
            class="send-sms-btn"
            round
            size="small"
            type="default"
            native-type="button"
          >
            <van-count-down
              :time="time"
              @finish="isShow = false"
              format="ss s"
            />
          </van-button>
          <!-- 发送验证码按钮 -->
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            type="default"
            native-type="button"
            @click="sendMsg"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info">
          登录
        </van-button>
      </div>
    </van-form>

    <!-- /登录表单 -->
  </div>
</template>

<script>
import { login, sendCode } from "@/api/user";

export default {
  name: "LoginIndex",
  components: {},
  props: {},
  data() {
    return {
      // 表单信息对象
      user: {
        mobile: "18813012946",
        code: "246810"
      },
      // 表单校验规则对象
      userFormRules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空"
          },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: "手机号格式错误"
          }
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空"
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误"
          }
        ]
      },
      time: 60 * 1000,
      isShow: false
    };
  },
  computed: {},
  watch: {},

  methods: {
    async onSubmit(values) {
      this.$toast.loading({
        message: "登陆中...",
        forbidClick: true, //禁止背景点击
        duration: 0
      });
      try {
        let { data: res } = await login(values);
        console.log(res);
        this.$store.commit("setUser", res.data);
        this.$toast.success("登陆成功");
        // 跳转页面
        console.log(this.$route);
        this.$router.replace(this.$route.query.redirect || "/user");
      } catch (error) {
        if (error.response.status === 400) {
          this.$toast.fail("手机号或验证码不正确");
        } else {
          this.$toast.fail("服务器繁忙，稍后重试");
        }
      }
    },
    async sendMsg() {
      try {
        this.$refs.form.validate("mobile");
      } catch (error) {
        this.$toast.fail("手机号格式错误");
      }

      // 显示倒计时
      this.isShow = true;
      try {
        await sendCode(this.user.mobile);
        this.$toast.success("发送成功");
      } catch (error) {
        this.$toast.fail("发送验证码失败，稍后重试");
      }
    }
  }
};
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 178px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
